package org.penguin.study.bk.task.controller;

import org.penguin.study.bk.base.vo.Result;
import org.penguin.study.bk.task.SimpleJob;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * https://baeldung-cn.com/spring-quartz-schedule
 * @author chengyinghua
 */
@RestController
@RequestMapping("/task")
public class TaskController {

    private final String className = "org.penguin.study.bk.task.SimpleJob";

    private final String jobName = className + "JobDetail";
    private final String jobGroup = "simpleGroup";
    private final String cronExpression = "10 3 * * * ?";

    private final String triggerName = className + "Trigger";
    private final String triggerGroup = className + "simpleGroupTrigger";

    @Autowired
    private Scheduler scheduler;

    @GetMapping("/start")
    public Result<?> start() {
        JobDetail simpleJobDetail = null;
        try {
            JobKey jobKey = new JobKey(jobName, jobGroup);
            // 如果存在这个任务，则删除
            if (scheduler.checkExists(jobKey)) {
                scheduler.deleteJob(jobKey);
            }
            simpleJobDetail = JobBuilder.newJob((Class<? extends Job>) Class.forName(className)).withIdentity(jobKey).usingJobData("name", "程英华").build();

//        Seconds(0~59) Minutes(0~59) Hours(0~23) DayofMonth(1~31) Month(1~12) DayofWeek(1~7) Year(1970~2099)
            Trigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(triggerName, triggerGroup).withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)).build();
            scheduler.scheduleJob(simpleJobDetail, cronTrigger);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return Result.error(String.format("找不到定时任务类: %s", className));

        } catch (SchedulerException e) {
            e.printStackTrace();
            return Result.error(String.format("启动定时任务异常: %s", e.getMessage()));
        }
        return Result.ok(String.format("成功启动定时任务: ", SimpleJob.class.getName()));
    }

    @GetMapping("/pause")
    public Result<?> stop() {
        JobDetail simpleJobDetail = null;
        try {
            JobKey jobKey = new JobKey(jobName, jobGroup);

            simpleJobDetail = scheduler.getJobDetail(jobKey);
            if (simpleJobDetail != null) {
                scheduler.pauseJob(jobKey);
            }
            scheduler.shutdown();
            scheduler.isShutdown();
            scheduler.isStarted();
        } catch (SchedulerException e) {
            e.printStackTrace();
            return Result.error(String.format("停止定时任务异常: %s", e.getMessage()));
        }
        return Result.ok(String.format("成功停止定时任务: ", SimpleJob.class.getName()));
    }

    @GetMapping("/resume")
    public void resumeJob() throws SchedulerException {
        JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (jobDetail == null) {
            return;
        }
        scheduler.resumeJob(jobKey);
    }

    @GetMapping("/remove")
    public void removeJob() throws SchedulerException {
        TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroup);
        JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
        Trigger trigger = scheduler.getTrigger(triggerKey);
        if (trigger == null) {
            return;
        }
        // 停止触发器
        scheduler.pauseTrigger(triggerKey);
        // 移除触发器
        scheduler.unscheduleJob(triggerKey);
        // 删除任务
        scheduler.deleteJob(jobKey);
    }
}
